package com.igeek.ch02;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @package com.igeek.ch02
 * @Description 校验正数的切面
 * @Author LittlePunk [296526219@qq.ccom]
 * @Version 2022.1
 * @Date 2023/11/15 16:30
 */
@Component
@Aspect
@Order(1)
public class CheckPositive {
    @Around("LogAspect.pt()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint){
        //校验正数

        //获取方法名
        String methodName = joinPoint.getSignature().getName();
        //获取参数
        Object[] args = joinPoint.getArgs();
        int length = args.length;
        //判断参数的合法性
        int size = Stream.of(args).filter(arg -> (int)arg>0)
                .collect(Collectors.toList()).size();
        //执行方法  否则不执行
        Object result = 0;
        if (length == size){
            try {
                result = joinPoint.proceed();
            } catch (Throwable e) {
                throw new RuntimeException(e);
            }
        }else{
            System.out.println("校验参数非法");
            return 0;
        }
        return result;
    }
}
